package net.hctek.identity.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;


import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;

import net.hctek.identity.util.ChineseUtill;
import net.hctek.identity.util.PropertyUtil;

public class FileDownloadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String filename;
	private String ipAddress;
	private static String filename_temp;
	private String downloadFileName;
	
	public 	String execute(){
		return SUCCESS;
	}
	
	public InputStream getInputStream(){
		
		
		PropertyUtil propertyUtil = new PropertyUtil();
		if(ChineseUtill.isMessyCode(filename)){
			setFilename(getFilename());
			System.out.println("true"+filename);
		}else{
			System.out.println("false"+filename);
		}
		//获取路径
		String path = ServletActionContext.getServletContext().getRealPath("/upload/"+"Musiclibrary");
		String FilePath = path+"/"+filename;
		System.out.println("FilePath:"+FilePath);
		File file = new File(FilePath);
		if(!filename.equals(filename_temp) && filename != null){
			filename_temp = filename;
			System.out.println("filename_temp"+filename_temp);
			if(ipAddress != null){
				propertyUtil.setProperty("ipAddress", ipAddress);
				propertyUtil.setProperty("songName", filename_temp);
			}
		}
		try {
			return  FileUtils.openInputStream(file);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("getInputStream:"+"下载出错");
		}
		return null;
	}
	
	public String getDownloadFileName()  {
		String downFileName = filename;
		try {
			downFileName = new String(downFileName.getBytes("UTF-8"),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		return downFileName;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

	public String getFilename() {
		try {
			filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");
			return filename;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	
}
